﻿namespace ManagerClinic.DanhMuc
{
    using System.Xml;
    using System.Runtime.InteropServices;
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using DevExpress.XtraEditors;
    using ManagerClinic.Core.Common;
    using ManagerClinic.Core.Business;
    using ManagerClinic.Modules;
    using ManagerClinic.Ultilities.Ultility;

    public partial class frmCapNhatDmDonVi : DevExpress.XtraEditors.XtraForm
    {
        public event EventHandler dmDonViEvent;
        int State = 0;

        ThietLapMaBusiness thietLapMaBusiness = new ThietLapMaBusiness();
        DmDonViBusiness dmDonViBusiness = new DmDonViBusiness();
        DmDonViEntity objDmDonViEntity = null;

        public frmCapNhatDmDonVi(int state)
        {
            InitializeComponent();
            State = state;
        }

        public frmCapNhatDmDonVi(int state, DmDonViEntity obj)
        {
            InitializeComponent();
            State = state;
            objDmDonViEntity = obj;

            txtMaDonVi.Text = objDmDonViEntity.MaDonVi;
            txtTenDonVi.Text = objDmDonViEntity.TenDonVi;
            txtMoTa.Text = objDmDonViEntity.MoTa;
            chkTrangThaiSuDung.Checked = objDmDonViEntity.TrangThaiSuDung;
            txtMaDonVi.Enabled = false;
            txtTenDonVi.Focus();
        }

        private void frmcapnhatDMdonvi_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SendKeys.SendWait("{Tab}");
            }
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (validateManager1.IsValid() == false) return;
            if (Validation.ValidSpaceTextBoxKey(txtMaDonVi, "Mã đơn vị tính") == false) return;

            objDmDonViEntity = new DmDonViEntity()
            {
                MaDonVi = txtMaDonVi.Text,
                TenDonVi = txtTenDonVi.Text.Trim(),
                MoTa = txtMoTa.Text.Trim(),
                TrangThaiSuDung = (bool)chkTrangThaiSuDung.Checked
            };

            if (this.State == (int)ConstantManagerClinic.StateInsertOrUpdate.Insert)
            {
                if (dmDonViBusiness.GetDmDonVi(txtMaDonVi.Text.Trim()) != null)
                {
                    if (Validation.ValidPrimaryKey(txtMaDonVi, "Mã đơn vị tính") == false) return;
                }
                dmDonViBusiness.InsertDmDonVi(objDmDonViEntity);
                Validation.ClearControl(this);
                txtMaDonVi.Focus();
            }
            else
            {
                dmDonViBusiness.EditDmDonVi(objDmDonViEntity);
                MessageBox.Show("Cập nhật thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            if (dmDonViEvent != null)
                dmDonViEvent(sender, e);
        }

        private void frmcapnhatDMdonvi_Load(object sender, EventArgs e)
        {

        }

    }
}